package com.wxlsoft.config.model;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 服务定义
 * 
 * @author wangxiaoliang
 */
public enum ConfigFileType {

	SERVICE(0, "serviceDefineFiles", "serviceDefineFiles.properties", "service_(\\d+)V(.*)\\.xml", "服务定义"),
	CONSUMER_UNPACK(1, "ConsumerMessageUnpack", "ConsumerMessageUnpack.properties", "cons_(\\d+)V(.*)_from_(.*)\\.xml", "客户端拆组"),
	CONSUMER_PACKER(2, "ConsumerMessagePacker", "ConsumerMessagePacker.properties", "cons_(\\d+)V(.*)_to_(.*)\\.xml", "客户端组包"),
	PROVIDER_UNPACK(3, "ProviderMessageUnpack", "ProviderMessageUnpack.properties", "prvd_(\\d+)V(.*)_from_(.*)\\.xml", "服务端拆组"),
	PROVIDER_PACKER(4, "ProviderMessagePacker", "ProviderMessagePacker.properties", "prvd_(\\d+)V(.*)_to_(.*)\\.xml", "服务端组包");

	private int type;

	private String namespace;

	private String name;

	private String desc;

	private Pattern pattern;

	private ConfigFileType(int type, String name, String namespace, String pattern, String desc) {
		this.type = type;
		this.name = name;
		this.namespace = namespace;
		this.pattern = Pattern.compile(pattern);
		this.desc = desc;
	}

	public int getType() {
		return type;
	}

	public String getNamespace() {
		return namespace;
	}

	public String getName() {
		return name;
	}

	public String getDesc() {
		return desc;
	}

	public static ConfigFileType parse(String filename) {
		ConfigFileType[] values = values();
		for (ConfigFileType type : values) {
			if (type.pattern.matcher(filename).find()) {
				return type;
			}
		}
		return null;
	}

	public static ConfigFileTypeValue entity(String filename) {
		Matcher matcher = SERVICE.pattern.matcher(filename);
		if (matcher.find()) {
			return new ConfigFileTypeValue(matcher.group(1), matcher.group(2), SERVICE);
		}
		
		matcher = CONSUMER_UNPACK.pattern.matcher(filename);
		if (matcher.find()) {
			return new ConfigFileTypeValue(matcher.group(1), matcher.group(2), CONSUMER_UNPACK);
		}
		
		matcher = CONSUMER_PACKER.pattern.matcher(filename);
		if (matcher.find()) {
			return new ConfigFileTypeValue(matcher.group(1), matcher.group(2), CONSUMER_PACKER);
		}
		
		matcher = PROVIDER_PACKER.pattern.matcher(filename);
		if (matcher.find()) {
			return new ConfigFileTypeValue(matcher.group(1), matcher.group(2), PROVIDER_PACKER);
		}
		
		matcher = PROVIDER_UNPACK.pattern.matcher(filename);
		if (matcher.find()) {
			return new ConfigFileTypeValue(matcher.group(1), matcher.group(2), PROVIDER_UNPACK);
		}
		return null;
	}

}
